define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    
    
    // require('globaljs');
    var rk_userService = require('userService');
    $.widget('rk.favoritesCreateNewPanelCtrl', $.rk.oaDialogCtrl, {
        _create: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var params = opt.params;

            var temp = require("oa/tmpl/favorites/content/tpl_favorites_create.tpl");
            elem.html(rk.templateText(temp));
            elem.find("[act='favorites_user_add_text']").snsmultipeopleselector({
                position: {
                    my: 'left top+2'
                }
            });
            if (params) {
                if (params.name) {
                    $(elem.find('[act="favorites_name"]')).val(params.name);
                }
                if (params.userIdsAdded) {

                    me._setSelectedUsers(params.userIdsAdded, params.usersAdded);
                }
            }
        },
        _setSelectedUsers: function(userIdsAdded, users_added) {
            var me = this;
            var elem = me.element;
            var userIds = userIdsAdded.split(",");
            var users = {};
            $.each(userIds, function(i, userId) {
                var user = me._getUserInfo(userId, users_added);

                var userParam = {};
                userParam.value = user.id;
                userParam.label = user.name;
                userParam.icon = user.icon;

                users[i] = userParam;
            });
            elem.find('[act="favorites_user_add_text"]').snsmultipeopleselector('setItems', users);
        },
        _getUserInfo: function(userId, users) {
            var res = {};
            $.each(users, function(i, user) {
                if (userId == user.id) {
                    res.id = user.id;
                    res.name = user.name;
                    res.icon = user.icon || rk_userService.DEFAULT_USER_ICON;
                }
            });
            return res;
        },
        _init: function() {
            var me = this;
            var footer = me.getFooter();
            me.getBtn('ok').on('click', $.proxy(me.okClicked, me));
            footer.find('*[act="cancel"]').on('click', $.proxy(me.cancelClicked, me));
        },
        okClicked: function() {
            var me = this;
            me._doSave();//保存
        },
        cancelClicked: function() {
            var me = this;
            me.closeMe();//关闭
        },
        _doCreate: function() {
            var me = this;
            var elem = me.element;
            var params = {};
            var favoritesName = elem.find('[act="favorites_name"]').val();
            params.name = favoritesName;

            var users = elem.find('[act="favorites_user_add_text"]').snsmultipeopleselector("val");
            var userIds = "";
            $.each(users, function(i, user) {
                if (user) {
                    userIds += user + ",";
                }
            });

            if (!params.name) {
                rk.noticeError("请输入收藏夹名称");
                return;
            } else {
                if (params.name.length > 30) {
                    rk.noticeError("名称最长为30个字");
                    return;
                }
            }

            if (userIds && userIds.length) {
                params.userIdsAdded = userIds.substring(0, userIds.length - 1);
                if (params.userIdsAdded.split(",").length > 30) {
                    rk.noticeError("收藏夹人员不超过30人");
                    return;
                }
            } else {
                rk.noticeError("请选择收藏夹人员");
                return;
            }

            me.getBtn('ok').beLoading();
            rk.post("/json/oa_favorites/save_favorites.action", params, function(json) {
                if (0 === json.status && json.favoritesId) {
                    me.closeMe();
                    rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                    me.getBtn('ok').unLoading();
                    //loaddataType
                    if(rk.favoritesPanelCtrl){
                        require.async(["../favoritesPanelCtrl"], function() {
                            rk.favoritesPanelCtrl("loaddata", "favorites");
                        });
                    }
                } else if (50000 === json.status) {
                    rk.noticeError("该名称已存在");
                    me.getBtn('ok').unLoading();
                } else {
                    rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                }
            });
            me.getBtn('ok').unLoading();
        },
        _doModify: function(favoritesId) {
            var me = this;
            var elem = me.element;
            var params = {};
            var favoritesName = elem.find('[act="favorites_name"]').val();

            params.favoritesId = favoritesId;
            params.name = favoritesName;
            params.userIdsAdded = "";

            var users = elem.find('[act="favorites_user_add_text"]').snsmultipeopleselector("val");
            var userIds = "";
            $.each(users, function(i, user) {
                if (user) {
                    userIds += user + ",";
                }
            });

            if (!params.name) {
                rk.noticeError("请输入收藏夹名称");
                return;
            } else {
                if (params.name.length > 30) {
                    rk.noticeError("名称最长为30个字");
                    return;
                }
            }

            if (userIds && userIds.length) {
                params.userIdsAdded = userIds.substring(0, userIds.length - 1);
                if (params.userIdsAdded.split(",").length > 30) {
                    rk.noticeError("收藏夹人员不超过30人");
                    return;
                }
            } else {
                rk.noticeError("请选择收藏夹人员");
                return;
            }
            me.getBtn('ok').beLoading();
            rk.post("/json/oa_favorites/modify_favorites.action", params, function(json) {
                if (0 === json.status && json.success) {
                    me.closeMe();
                    rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                    me.getBtn('ok').unLoading();
                } else if (50000 === json.status) {
                    rk.noticeError("该名称已存在");
                    me.getBtn('ok').unLoading();
                } else {
                    rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                }
            });

        },
        _doSave: function() {
            var me = this;
            var opt = me.options;
            var params = opt.params;
            if (params && params.favoritesId) {
                me._doModify(params.favoritesId);
            } else {
                me._doCreate();
            }
        }
    });
});